package com.yanqu.road.entity.activity.sonhai.config;

import com.yanqu.road.entity.activity.cat.Position;
import com.yanqu.road.entity.activity.pawnshop.PawnshopConfig;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiXYData;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiMapTypeEnum;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.plot.PlotUtil;
import com.yanqu.road.utils.plot.Point;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;

import java.util.*;
import java.util.logging.Logger;

public class SonHaiMapConfig {

    private static org.apache.logging.log4j.Logger logger = LogManager.getLogger(SonHaiMapConfig.class.getName());

    //活动ID
    private int activityId;

    //区域ID
    private int floor;

    //ID
    private int id;

    //地图参数1=兽王，2=非兽王，0=空地块（左上角0,0）x轴，y轴=地块类型
    private String param;
    //k:地块类型，v:地块类型对应的地块坐标，x,y
    private Map<Integer, Set<String>> paramMap;
    private int maxX;
    private int maxY;

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public int getFloor() {
        return floor;
    }

    public void setFloor(int floor) {
        this.floor = floor;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
        int maxX = 0;
        int maxY = 0;
        if (!StringUtils.isNullOrEmpty(param)) {
            Map<Integer, Set<String>> paramMap = new HashMap<>();
            String[] params = param.split("\\|");
            for (String item : params) {
                String[] itemArr = item.split("=");
                int type = Integer.parseInt(itemArr[1]);
                String position = itemArr[0];
                Set<String> positionSet = paramMap.computeIfAbsent(type, k -> new HashSet<>());
                positionSet.add(position);
                String[] xyArr = position.split(",");
                int x = Integer.parseInt(xyArr[0]);
                int y = Integer.parseInt(xyArr[1]);
                if (x > maxX) {
                    maxX = x;
                }
                if (y > maxY) {
                    maxY = y;
                }
            }
            this.paramMap = paramMap;
        }
    }

    public int getMaxX() {
        return maxX;
    }

    public int getMaxY() {
        return maxY;
    }

    /**
     * 随机获取boss的4个地块
     * @return
     */
    public List<SonHaiXYData> randomBossPosition() {
        //随机出一个类型为 1 的地块
        Set<String> bossPositionSet = this.paramMap.get(SonHaiMapTypeEnum.MAP_TYPE_BOSS.getType());
        String bossPosition = RandomHelper.getRandomElement(bossPositionSet);
        if (bossPosition == null) {
            logger.error("山海伏兽-无配置boss地块:{}", bossPositionSet);
            bossPosition = "5,5";
        }
        //构造一下boss的4个地块
        String[] bossPositionArr = bossPosition.split(",");
        int x = Integer.parseInt(bossPositionArr[0]);
        int y = Integer.parseInt(bossPositionArr[1]);
        //上方、右侧、右上斜方的三个坐标点组成的列表
        List<Point> adjacentCoordinates = PlotUtil.getAdjacentCoordinates(x, y);
        List<SonHaiXYData> positionList = new ArrayList<>();
        positionList.add(new SonHaiXYData(x, y));
        for (Point point : adjacentCoordinates) {
            SonHaiXYData sonHaiXYData = new SonHaiXYData();
            sonHaiXYData.setX(point.getX());
            sonHaiXYData.setY(point.getY());
            positionList.add(sonHaiXYData);
        }
        return positionList;
    }

    /**
     * 判断坐标是否合法，在类型 1,2为合法地块
     * @param x
     * @param y
     * @return
     */
    public boolean checkPosition(int x, int y) {
        String position = x + "," + y;
        //兽王地块
        Set<String> bossPositionSet = this.paramMap.get(SonHaiMapTypeEnum.MAP_TYPE_BOSS.getType());
        if (bossPositionSet != null && bossPositionSet.contains(position)) {
            return true;
        }
        //普通地块
        Set<String> normalPositionSet = this.paramMap.get(SonHaiMapTypeEnum.MAP_TYPE_NORMAL.getType());
        if (normalPositionSet != null && normalPositionSet.contains(position)) {
            return true;
        }
        return false;
    }
}
